package com.inspur.ehcache.service.impl;

import com.inspur.ehcache.entity.User;
import com.inspur.ehcache.service.UserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@CacheConfig(cacheNames="users")
public class UserServiceImpl implements UserService {

    @Override
    @Cacheable(value = "users",key = "#id")
    public User selectUserById(int id) {
        System.out.println("find data from db!!!");
        User user = new User();
        user.setId(1);
        user.setName("Malcolm");
        user.setAge(24);
        return user;
    }

    @Override
    @CachePut(key = "#user.id")
    public User updateUserById(User user) {
        return user;
    }

    @Override
    @CacheEvict()
    public void deleteUserById(Integer id) {
        //在这里执行删除操作， 删除是去数据库中删除
    }

}